package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequestMethod;
	import flash.text.TextField;
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author Joseph Waters
	 */
	public class Database extends Sprite
	{		
		private var dataReturned:String = new String();
		
		private var curDate:Date = new Date();
		
		private var testText:TextField = new TextField();
		private var testInput:TextField = new TextField();
		private var testInputBox:MovieClip = new MovieClip();
		
		private var hoverMenu:GuiMenu;
		
		// Actual database variables
		private var textSender:DBMessaging = new DBMessaging();
		private var menuMessager:DBMessaging = new DBMessaging();
		
		public function Database() 
		{		
			var testMe:SolarSystem = new SolarSystem(4, 200, 200);
			this.addChild(testMe);
			
			// Add text and a text input field for testing
			// Test output
			testText.x = 20;
			testText.y = 20;
			testText.width = 140;
			testText.text = "Press enter to submit.";
			testText.textColor = 0xFFFFFF;
			this.addChild(testText);
			
			// Test dropdown menu
			hoverMenu = new GuiMenu(testText.x + testText.width + 10, testText.y, 0xDFDFDF, 10, "hover");
			hoverMenu.addItem("Hover to view queue entries.", 1, -1, "base", true);
			hoverMenu.addItem("Test1", 2, 1, "item1", false);
			hoverMenu.addItem("Test2", 2, 1, "item2", false);
			hoverMenu.addItem("Test2", 2, 1, "item3", false);
			this.addChild(hoverMenu);
			hoverMenu.addEventListener(MouseEvent.MOUSE_OVER, menuLoad, false, 0, false);
			
			// Test input
			testInput.x = 20;
			testInput.y = 50;
			testInput.width = 140;
			testInput.height = 20;
			testInput.maxChars = 20;
			testInput.text = "Input text here.";
			testInput.type = "input";
			testInput.textColor = 0x111111;
			testInputBox.graphics.beginFill(0xFFFFFF);
			testInputBox.graphics.drawRect(20, 50, 140, 20);
			testInputBox.graphics.endFill();
			testInputBox.width = 140;
			testInputBox.height = 20;
			testInputBox.addChild(testInput);
			this.addChild(testInputBox);
			
			testInputBox.addEventListener(MouseEvent.MOUSE_UP, function(e:Event):void { testInput.text = "" }, false, 0, false);
			testInput.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void { if (e.keyCode == 13) { testDataSend(testInput.text); } }, false, 0, false);
			// Test text stuff ends here
			
			// Add checks here on object creation. Does this game exist in the database?
			// Does the database exist?
			// Can I query the database?
			// Does this user/playerId exist in the database?
		}
		
		private function testDataSend(dataToSend:String):void {
			curDate = new Date();
			var timeStamp:String = String(curDate.getHours()) + String(curDate.getMinutes()) + String(curDate.getSeconds());
			timeStamp = timeStamp + String(curDate.getFullYear()) + String(curDate.getMonth()) + String(curDate.getDate());
			dataToSend = "newId=1&newInfo=" + dataToSend + "&newTime=" + timeStamp;
			textSender.addEventListener(Event.COMPLETE, getTextSendResponse);
			textSender.sendData(dataToSend);
			testText.text = "Loading...";
		}
		
		private function getTextSendResponse(e:Event):void {
			var responseList:String = textSender.dataReturned;
			testText.text = responseList;
		}
		
		private function menuLoad(e:MouseEvent):void {
			hoverMenu.removeSubItems();
			menuMessager.addEventListener(Event.COMPLETE, menuUpdate);
			menuMessager.getData();
		}
		
		private function menuUpdate(e:Event):void {
			var infoList:String = menuMessager.dataReturned;
			var regExp:RegExp = /\W+/g;
			var infoArray:Array = infoList.split("&");
			for (var a:uint = 0; a < infoArray.length - 1; a++) {
				hoverMenu.addItem(infoArray[a], 2, 1, infoArray[a], true);
			}
		}
	}

}